var preload;

function init() {
	if (window.top != window) {
		document.getElementById("header").style.display = "none";
	}

	// Create a new queue.
	preload = new createjs.PreloadJS();
	preload.installPlugin(createjs.SoundJS);
	preload.onFileLoad = handleFileLoaded;
	preload.onError = handleFileError;
}

function stop() {
	if (preload != null) {
		preload.close();
	}
}

// Load a single asset.
function loadAsset(target) {
	var div = document.getElementById(target.id);
	div.innerHTML = "<label>Loading...</label>";
	preload.loadFile(target.id);
}

// Once each file is loaded, show it. Each ID corresponds to the related DIV.
function handleFileLoaded(event) {
	var div = document.getElementById(event.id);
	switch (event.type) {
		case createjs.PreloadJS.CSS :
			var head = document.getElementsByTagName('head')[0];
			head.appendChild(event.result);
			div.innerHTML = "<label>Complete :)</label>";
			break;

		case createjs.PreloadJS.IMAGE :
			div.innerHTML = "<img src='" + event.id + "' width=" + div.clientWidth + " height=" + div.clientHeight + "/>";
			break;

		case createjs.PreloadJS.JAVASCRIPT :
			console.log(typeof event.result);
			document.body.appendChild(event.result);
			div.innerHTML = "<label>Complete :)</label>";
			break;

		case createjs.PreloadJS.JSON :
		case createjs.PreloadJS.XML :
		case createjs.PreloadJS.TEXT :
			//alert(event.result);
			console.log(event.result);
			$("body").append(event.result);
			//console.log($("#tpl_status").html());
			div.innerHTML = "<label>Complete :)</label>";
			break;

		case createjs.PreloadJS.SOUND :
			document.body.appendChild(event.result);
			event.result.play();
			div.innerHTML = "<label>Complete :)</label>";
			break;
	}
	div.style.backgroundColor = "#222222";
}

// A file failed to load.
function handleFileError(result) {
	var div = document.getElementById(result.id);
	div.innerHTML = "<label>Error :(</label>";
	div.style.backgroundColor = "#992222";
}
